package com.krtech.wecard.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.nosql.redis.RedisDS;
import cn.hutool.setting.Setting;
import cn.hutool.system.SystemUtil;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

@Component
public class WecardRedisTemplate {
	private Jedis jedis;
	private RedisDS redisDS;

	public WecardRedisTemplate(){
		Setting setting = new Setting();
		setting.set("host", SystemUtil.get("XF_IP","10.10.4.154"));
//		setting.set("password", SystemUtil.get("REDIS_PASSWORD","MySql684W3JpuKsI"));
		String redisPassword = SystemUtil.get("REDIS_PASSWORD", "MySql684W3JpuKsI");
		if (StrUtil.isNotEmpty(redisPassword)){
			setting.set("password", redisPassword);
		}

		setting.set("port", SystemUtil.get("REDIS-PORT","6379"));
		setting.set("testOnBorrow", SystemUtil.get("TEST-ON-BORROW",true));
		setting.set("testOnReturn", SystemUtil.get("TEST-ON-RETURN",true));
		setting.set("database",SystemUtil.get("DATABASE","1"));
		redisDS = RedisDS.create(setting, null);
		jedis = redisDS.getJedis();
	}

	public void set(String key,String value){
		Jedis nowJedis = redisDS.getJedis();

		try {
			nowJedis.set(key,value);
		} catch (Exception e) {
			//释放redis对象
			nowJedis.close();;
			e.printStackTrace();
		} finally {
			//返还到连接池
			nowJedis.close();
		}
	}

	/**
	 * 设置成功，返回 1 。设置失败，返回 0
	 * @param key
	 * @param value
	 * @return
	 */
	public Long setNx(String key,String value){
		Jedis nowJedis = redisDS.getJedis();

		try {
			Long setnx = nowJedis.setnx(key, value);
			return setnx;
		} catch (Exception e) {
			//释放redis对象
			nowJedis.close();;
			e.printStackTrace();
		} finally {
			//返还到连接池
			nowJedis.close();
		}
		return null;
	}

	/**
	 *
	 * @param key
	 * @param value
	 * @param time:单位，秒
	 */
	public void set(String key,String value,int time){
		Jedis nowJedis = redisDS.getJedis();

		try {
			nowJedis.setex(key,time,value);
		} catch (Exception e) {
			//释放redis对象
			nowJedis.close();;
			e.printStackTrace();
		} finally {
			//返还到连接池
			nowJedis.close();
		}
	}

	public String get(String key){
		Jedis nowJedis = redisDS.getJedis();

		String getResult = null;

		try {
			getResult = nowJedis.get(key);
		} catch (Exception e) {
			//释放redis对象
			nowJedis.close();;
			e.printStackTrace();
		} finally {
			//返还到连接池
			nowJedis.close();
		}
		return getResult;
	}

	public void del(String key){
		Jedis nowJedis = redisDS.getJedis();

		try {
			nowJedis.del(key);
		} catch (Exception e) {
			//释放redis对象
			nowJedis.close();;
			e.printStackTrace();
		} finally {
			//返还到连接池
			nowJedis.close();
		}
	}

	/**
	 * 增
	 */
	public Long incr(String key){
		Long incr = null;
		Jedis nowJedis = redisDS.getJedis();

		try {
			incr = nowJedis.incr(key);
		} catch (Exception e) {
			//释放redis对象
			nowJedis.close();;
			e.printStackTrace();
		} finally {
			//返还到连接池
			nowJedis.close();
		}

		return incr;
	}

	/**
	 * 减
	 */
	public Long decr(String key){
		Long incr = null;
		Jedis nowJedis = redisDS.getJedis();

		try {
			incr = nowJedis.decr(key);
		} catch (Exception e) {
			//释放redis对象
			nowJedis.close();;
			e.printStackTrace();
		} finally {
			//返还到连接池
			nowJedis.close();
		}

		return incr;
	}

	/**
	 * close
	 */
	public void close(){
		jedis.close();
	}

}
